#include "datainfo.h"

DataInfo::DataInfo(QObject *parent) : QObject(parent)
{

}

DataInfo::~DataInfo()
{

}

//反序列化
void DataInfo::inparse(QByteArray bytearray)
{
    qDebug() << "nihao";
    QJsonParseError jsonError;
    QJsonDocument paserDoc = QJsonDocument::fromJson(bytearray,&jsonError);
    if (jsonError.error == QJsonParseError::NoError)
    {
        QJsonObject paserObj = paserDoc.object();
        if (paserObj.contains("type")) {
            QJsonValue typeValue = paserObj.take("type");
            if (typeValue.isDouble()) {
                qDebug() << "type: " << typeValue.toInt();
            }
        }
        if (paserObj.contains("result")) {
            QJsonValue resultValue = paserObj["result"];
            if (resultValue.isDouble())
                qDebug() << "result: " << resultValue.toInt();
        }
        if(paserObj.contains("data"))
        {
            QJsonValue value = paserObj.value("data");
            if(value.isArray())
            {
                QJsonArray array = value.toArray();
                int nSize = array.size();
                for(int i = 0; i < nSize; ++i)
                {
                    int ArrayKey;
                    QString ArrayVal;
                    QJsonValue value = array.at(i);
                    if(value.isObject())
                    {
                        QJsonObject subobj = value.toObject();
                        if(subobj.contains("key"))
                        {
                            QJsonValue subValue = subobj.value("key");
                            ArrayKey = subValue.toInt();
                            qDebug() << "key : " << ArrayKey;
                        }
                        if(subobj.contains("val"))
                        {
                            QJsonValue subValue = subobj.value("val");
                            ArrayVal = subValue.toString();
                            qDebug() << "val : " << ArrayVal;
                        }
                    }
                    emit keyValueOut(ArrayKey,ArrayVal);
                }
            }
        }
    }
}

void DataInfo::publishMessage(QJsonObject obj)
{
    QJsonDocument doc(obj);
    QByteArray json = doc.toJson();
    //创建发送的message
    QMQTT::Message message;
    message.setPayload(json);
    //指定发送的发送报文
    message.setTopic("putData");
    qDebug() << message.payload();
    qDebug() << message.topic();

    GlobalVal::mqtt->publish(message);
}

void DataInfo::fanJsonParse(QByteArray bytearray)
{
    QJsonParseError jsonError;
    QJsonValue keyValue;
    QJsonDocument paserDoc = QJsonDocument::fromJson(bytearray,&jsonError);
    if (jsonError.error == QJsonParseError::NoError)
    {
        QJsonObject paserObj = paserDoc.object();
        if (paserObj.contains("type"))
        {
            QJsonValue typeValue = paserObj.take("type");
            if (typeValue.isDouble())
            {
                qDebug() << "type: " << typeValue.toInt();
            }
        }
        if (paserObj.contains("result"))
        {
            QJsonValue resultValue = paserObj["result"];
            if (resultValue.isDouble())
                qDebug() << "result: " << resultValue.toInt();
        }
        if (paserObj.contains("key"))
        {
            keyValue = paserObj.take("key");
            if (keyValue.isDouble())
            {
                qDebug() << "key: " << keyValue.toInt();
            }
        }

        if(paserObj.contains("data"))
        {
            QJsonValue value = paserObj.value("data");
            if(value.isArray())
            {
                QJsonArray array = value.toArray();
                int nSize = array.size();
                for(int i = 0; i < nSize; ++i)
                {
                    int ArrayKey;
                    QString ArrayVal;
                    QJsonValue value = array.at(i);
                    if(value.isObject())
                    {
                        QJsonObject subobj = value.toObject();
                        if(subobj.contains("time"))
                        {
                            QJsonValue subValue = subobj.value("time");
                            ArrayKey = subValue.toInt();
                            qDebug() << "time : " << ArrayKey;
                        }
                        if(subobj.contains("val"))
                        {
                            QJsonValue subValue = subobj.value("val");
                            ArrayVal = subValue.toString();
                            qDebug() << "val : " << ArrayVal;
                        }
                    }if(keyValue.toInt() == 200)
                    {
                        emit hisTemSignal(ArrayKey,ArrayVal);
                    }
                    else if(keyValue.toInt() == 201)
                    {
                        emit hisHumSignal(ArrayKey,ArrayVal);
                    }

                }
            }
        }
    }
}


